-# locals: instance, id_prefix, seen_ids, is_root

- if current_user.can_manage_risk?

  -model_underscore = instance.class.name.underscore

  -is_root = false if local_assigns[:is_root].nil?

  -seen_ids = [] if local_assigns[:seen_ids].nil?
  -id_prefix = "risks-widget-instance-#{instance.id}" if local_assigns[:id_prefix].nil?

  -seen_this = seen_ids.include?(instance.id)

  -sub_objects = instance.dependent_self_objects
  -risks_with_risky_attributes = instance.risks_with_risky_attributes

  -expand_this = (!seen_this && sub_objects.count > 0) || (!seen_this && risks_with_risky_attributes.count > 0)

  -summary_toggle_target = "#{id_prefix}-summary"
  -objects_toggle_target = "#{id_prefix}-objects"

  -main_toggle_target = expand_this ? objects_toggle_target : summary_toggle_target

  -dependence_relationship_type = "#{model_underscore}_relies_upon_#{model_underscore}"
  -risk_relationship_type = "risk_is_a_threat_to_#{model_underscore}"

  -has_dependence_relationship = instance.has_valid_relationship?(dependence_relationship_type)
  -has_risk_relationship = instance.has_valid_relationship?(risk_relationship_type)

  %li{ :class => instance.class.name.downcase, :'data-object-id' => instance.id }
    .item-main
      -if !is_root
        .openclose
      .item-title{ :class => is_root && 'active' }
        .row-fluid
          =render :partial => 'object_leaf', :locals => { :instance => instance, :object_type => 'business', :object_icon => "grcicon-#{instance.class.name.downcase}" }
        .item-actions
          %a.btn.btn-mini.halfopacity{ "rel" => "tooltip", "data-placement" => "left", "title" => "Edit this object", 'data-modal-class' => 'modal-wide', 'data-form-target' => 'refresh', "data-toggle" => "modal-ajax-form", :href => url_for(:id => instance.id, :action => :edit, :controller => model_underscore.pluralize, :only_path => true) }
            %i.grcicon-edit
          -if has_risk_relationship
            %a.btn.btn-mini.halfopacity{ :rel => "tooltip", :"data-placement" => "left", :title => "Link risks to this object", :'data-modal-class' => 'modal-wide', :'data-modal-reset' => 'reset', :'data-list-target' => 'refresh', :"data-toggle" => "modal-ajax-relationship-selector", :href => list_edit_flow_relationships_path(:object_id => instance.id, :object_type => instance.class.name, :related_model => 'Risk', :related_side => 'source', :relationship_type => risk_relationship_type) }
              %i.grcicon-link
          -if !is_root
            %a.btn.btn-mini.halfopacity{ "rel" => "tooltip", "data-placement" => "left", "title" => "Go to this object's page", :href => url_for(:id => instance.id, :action => :show, :controller => model_underscore.pluralize, :only_path => true) }
              %i.grcicon-arrowright-black
    .item-content{ :class => is_root && 'in' }
      -if !seen_this
        -if risks_with_risky_attributes.count > 0
          %ul.tree-structure
            -risks_with_risky_attributes.each do |risk, risky_attributes|
              =render :partial => 'risk_tree', :locals => { :risk => risk, :risky_attributes => risky_attributes, :id_prefix => "#{id_prefix}-risk-#{risk.id}" }
        -else
          .no-data
            No risks are linked to this object

        -if has_dependence_relationship && sub_objects.count > 0
          %ul.tree-structure
            -sub_objects.each_with_index do |sub_object, i|
              =render :partial => 'risk_object_tree', :locals => { :instance => sub_object, :id_prefix => "#{id_prefix}-sub-object-#{sub_object.id}", :seen_ids => seen_ids + [instance.id] }

